/**
 * This class makes the police station client objects
 * as UDP clients to communicate with eachother to send
 * receive record counts from one another.
 */
package com.comp6231.replica2.server;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

/**
 * @author Farzana Alam
 * 
 */
public class OperationClient {

	public static String getOthersOperation(String host, int port,
			String controlMsg) {
		DatagramSocket socket = null;
		// String countMsg = "COUNT";
		try {
			socket = new DatagramSocket();
			byte[] count = controlMsg.getBytes();
			InetAddress address = InetAddress.getByName(host);

			DatagramPacket request = new DatagramPacket(count,
					controlMsg.length(), address, port);
			socket.send(request);
			byte[] buffer = new byte[1000];
			DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
			socket.receive(reply);
			return new String(reply.getData()).trim();
		} catch (SocketException e) {
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			e.printStackTrace();
			return "";
		} finally {
			if (socket != null)
				socket.close();
		}
	}

}
